package com.sinocare.project.account.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.account.dao.SncUserLoginDao;
import com.sinocare.project.account.db.SystemAccountDb;
import com.sinocare.project.account.service.SccUserRoleService;
import com.sinocare.project.account.service.SccUserStoreService;
import com.sinocare.project.common.entity.*;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.account.service.SncUserLoginService;
import com.sinocare.project.common.util.SaltUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 用户登录表(SncUserLogin)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:54:53
 */
@RestController
@RequestMapping("sncUserLogin")
@CrossOrigin
public class SncUserLoginController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SncUserLoginService sncUserLoginService;

    /**
     * 服务对象
     */
    @Resource
    private SccUserStoreService sccUserStoreService;

    /**
     * 服务对象
     */
    @Resource
    private SccUserRoleService sccUserRoleService;
    /**
     * 初始化一个系统时间
     */
    Date date = new Date();



    /**
     * 分页查询所有数据
     *
     * @param page         分页对象
     * @param searchParams 查询实体
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll(Integer page , Integer limit, String searchParams) {
        QueryWrapper<SystemAccountDb> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(searchParams)){
            SystemAccountDb systemAccountDb = JSON.parseObject(searchParams, SystemAccountDb.class);
            wrapper.like(!systemAccountDb.getName().equals("")&& systemAccountDb.getName()!=null,"name",systemAccountDb.getName());
            wrapper.like(!systemAccountDb.getPhone().equals("")&& systemAccountDb.getPhone()!=null,"phone",systemAccountDb.getPhone());
            wrapper.like( systemAccountDb.getStatus()!=null,"b.status",systemAccountDb.getStatus());
            wrapper.eq( systemAccountDb.getProvince()!=null,"c.province",systemAccountDb.getProvince());
            wrapper.eq( systemAccountDb.getCity()!=null,"c.city",systemAccountDb.getCity());
            wrapper.eq( systemAccountDb.getCounty()!=null,"c.county",systemAccountDb.getCounty());
            System.out.println(systemAccountDb);
        }
        PageHelper.startPage(page,limit);
        List<SystemAccountDb> list = sncUserLoginService.queryAll(wrapper);
        for (SystemAccountDb s : list) {
            s.setSubStore(s.getStoreName()+" "+s.getProvince()+" "+s.getCity()+" "+s.getCounty());
        }
        PageInfo<SccDrugstore> info = new PageInfo(list);
        return Result.ok(ResultCode.SUCCESS,info.getTotal(),info.getList());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS,this.sncUserLoginService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param systemAccountDb 实体对象
     * @return 新增结果
     */
    @PostMapping(value = "ins")
    public Result insert(@RequestBody SystemAccountDb systemAccountDb) {
        //默认值增加
        System.out.println(systemAccountDb);
        //查询数据库最大账号
        QueryWrapper<SncUserLogin> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(count) as count");
        SncUserLogin one = sncUserLoginService.getOne(queryWrapper);
        System.out.println(one);
        String count = one.getCount().substring(6);
        int i = Integer.parseInt(count);
        ++i;
        systemAccountDb.setCount("S10000"+i);


        systemAccountDb.setCreateTime(date);
        systemAccountDb.setUpdateTime(date);
        SncUserLogin sncUserLogin = new SncUserLogin();
        sncUserLogin.setPassword("123456");
        SncUserLogin sncUserLogin1 = SaltUtil.encryptPassword(sncUserLogin, 5);
        systemAccountDb.setPassword(sncUserLogin1.getPassword());
        boolean save = sncUserLoginService.save(systemAccountDb);
        if(save){
            //用户也角色关系表的新增
            SccUserRole sccUserRole = new SccUserRole();
            sccUserRole.setUserId(systemAccountDb.getId());
            sccUserRole.setRoleId(systemAccountDb.getRoleId());
            System.out.println(sccUserRole);
            boolean Sur = sccUserRoleService.save(sccUserRole);
            //用户与店铺关系表的新增
            SccUserStore sccUserStore = new SccUserStore();
            sccUserStore.setUserId(systemAccountDb.getId());
            sccUserStore.setStoreId(systemAccountDb.getStoreId());
            System.out.println(sccUserStore);
            boolean Sus = sccUserStoreService.save(sccUserStore);
            if(Sur && Sus){
                return Result.ok(ResultCode.SUCCESS,true);
            }else{
                return Result.ok(ResultCode.FAIL,false);
            }
        }else{
            return Result.ok(ResultCode.FAIL,false);
        }
    }

    /**
     * 修改数据
     *
     * @param systemAccountDb 实体对象
     * @return 修改结果
     */
    @PutMapping("upd")
    public Result update(@RequestBody SystemAccountDb systemAccountDb) {
        UpdateWrapper<SccUserRole> updateWrapper1=new UpdateWrapper<>();
        //用户也角色关系表的新增
        SccUserRole sccUserRole = new SccUserRole();
        sccUserRole.setRoleId(systemAccountDb.getRoleId());
        System.out.println(sccUserRole);
        updateWrapper1.eq("user_id",systemAccountDb.getId());
        boolean Sur = sccUserRoleService.update(sccUserRole,updateWrapper1);
        //用户与店铺关系表的新增
        UpdateWrapper<SccUserStore> updateWrapper2=new UpdateWrapper<>();
        SccUserStore sccUserStore = new SccUserStore();
        sccUserStore.setStoreId(systemAccountDb.getStoreId());
        System.out.println(sccUserStore);
        updateWrapper2.eq("user_id",systemAccountDb.getId());
        boolean Sus = sccUserStoreService.update(sccUserStore,updateWrapper2);
        //对登录表数据就行修改
        systemAccountDb.setUpdateTime(date);
        systemAccountDb.setPwdLockDay(date);
        return Result.ok(ResultCode.SUCCESS,this.sncUserLoginService.updateById(systemAccountDb));
    }

    /**
     * 修改数据
     *
     * @param sncUserLogin 实体对象
     * @return 修改结果
     */
    @PutMapping("updSta")
    public Result update(@RequestBody SncUserLogin sncUserLogin) {
        return Result.ok(ResultCode.SUCCESS,this.sncUserLoginService.updateById(sncUserLogin));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS,this.sncUserLoginService.removeByIds(idList));
    }
}

